/////////////////////////////////////////////////////////////////////////
//
// Amuse Engine SDK - core/object
// Copyright (c) 2014.  All Rights Reserved
//
// File:		AEReferencedObject.inl
// Author:		Gianluca Belardelli
// Date:		09/10/2013
//
/////////////////////////////////////////////////////////////////////////
#ifndef _AEREFERENCEDOBJECT_INL_
#define _AEREFERENCEDOBJECT_INL_

AE_FORCEINLINE AEReferencedObject::AEReferencedObject( void ) : 
	m_uiMemSizeAndFlags(0xffff), m_uiReferenceCount(0) { }

AE_FORCEINLINE AEReferencedObject::AEReferencedObject( const AEReferencedObject &originalObject ) :
	AEBaseObject(), m_uiMemSizeAndFlags(0xffff), m_uiReferenceCount(0) 
{
}

AE_FORCEINLINE  AEReferencedObject &AEReferencedObject::operator=( const AEReferencedObject &originalObject )
{
	return *this;
}

AE_FORCEINLINE AEReferencedObject::~AEReferencedObject( void )
{
	AEASSERT_MSG( m_uiReferenceCount == 0, "Tentativo di eliminare un oggetto con contatore != 0" );
}

AE_FORCEINLINE AEINT32 AEReferencedObject::GetAllocatedSize( void ) const
{
	return m_uiMemSizeAndFlags & 0x7fff;
}

AE_FORCEINLINE AEINT32 AEReferencedObject::GetReferenceCount( void ) const
{
	return m_uiReferenceCount;
}

AE_FORCEINLINE void AEReferencedObject::AddRef( void ) const
{
	//m_uiReferenceCount++;
	AEAtomic::Increment( (AEINT32&)m_uiReferenceCount );
	printf("AEReferencedObject::AddRef %d\n", m_uiReferenceCount);
}

AE_FORCEINLINE void AEReferencedObject::Release( void )
{
	//m_uiReferenceCount--;
	AEASSERT( m_uiReferenceCount > 0 );
	AEAtomic::Decrement( (AEINT32&)m_uiReferenceCount );
	printf("AEReferencedObject::Release %d\n", m_uiReferenceCount);
	if( m_uiReferenceCount == 0 )
		DeleteThis();
}

AE_FORCEINLINE void AEReferencedObject::ReleaseNoDelete( void )
{
	AEASSERT( m_uiReferenceCount > 0 );
	AEAtomic::Decrement( (AEINT32&)m_uiReferenceCount );
}

AE_FORCEINLINE void AEReferencedObject::DeleteThis( void )
{
	delete this;
	//this->operator delete( this, this );
}

#endif // _AEREFERENCEDOBJECT_INL_
